<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 4.2.1">
  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">


<link rel="stylesheet" href="/lib/font-awesome/css/font-awesome.min.css">

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"yoursite.com","root":"/","scheme":"Gemini","version":"7.8.0","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":true,"show_result":true,"style":"flat"},"back2top":{"enable":true,"sidebar":true,"scrollpercent":true},"bookmark":{"enable":true,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},"path":"search.xml"};
  </script>

  <meta name="description" content="Git教程第一章：Git简介 CVS及SVN都是集中式的版本控制系统。 Git是分布式版本控制系统，极其强大的分支管理。  第二章：Git创建Git设置name和email123git config --global user.name &quot;Your Name&quot;git config --global user.email &quot;email@example.com&quot;		&#x2F;*注意&#96;git config&#96;命令">
<meta property="og:type" content="article">
<meta property="og:title" content="Git教程">
<meta property="og:url" content="http://yoursite.com/2020/03/13/Git/Git%E6%95%99%E7%A8%8B/index.html">
<meta property="og:site_name" content="heweiliang blog">
<meta property="og:description" content="Git教程第一章：Git简介 CVS及SVN都是集中式的版本控制系统。 Git是分布式版本控制系统，极其强大的分支管理。  第二章：Git创建Git设置name和email123git config --global user.name &quot;Your Name&quot;git config --global user.email &quot;email@example.com&quot;		&#x2F;*注意&#96;git config&#96;命令">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="c:%5CUsers%5CAdministrator%5CDesktop%5CStudyNote%5Cblogphoto%5C0.jpg">
<meta property="article:published_time" content="2020-03-13T02:16:42.878Z">
<meta property="article:modified_time" content="2020-06-26T08:15:31.354Z">
<meta property="article:author" content="heweiliang">
<meta property="article:tag" content="Git">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="c:%5CUsers%5CAdministrator%5CDesktop%5CStudyNote%5Cblogphoto%5C0.jpg">

<link rel="canonical" href="http://yoursite.com/2020/03/13/Git/Git%E6%95%99%E7%A8%8B/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-CN'
  };
</script>

  <title>Git教程 | heweiliang blog</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

<link rel="alternate" href="/atom.xml" title="heweiliang blog" type="application/atom+xml">
</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <h1 class="site-title">heweiliang blog</h1>
      <span class="logo-line-after"><i></i></span>
    </a>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>




<nav class="site-nav">
  <ul id="menu" class="menu">
        <li class="menu-item menu-item-home">

    <a href="/" rel="section"><i class="fa fa-fw fa-home"></i>首页</a>

  </li>
        <li class="menu-item menu-item-about">

    <a href="/about/" rel="section"><i class="fa fa-fw fa-user"></i>关于</a>

  </li>
        <li class="menu-item menu-item-tags">

    <a href="/tags/" rel="section"><i class="fa fa-fw fa-tags"></i>标签</a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/categories/" rel="section"><i class="fa fa-fw fa-th"></i>分类</a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/archives/" rel="section"><i class="fa fa-fw fa-archive"></i>归档</a>

  </li>
        <li class="menu-item menu-item-schedule">

    <a href="/schedule/" rel="section"><i class="fa fa-fw fa-calendar"></i>日程表</a>

  </li>
        <li class="menu-item menu-item-sitemap">

    <a href="/sitemap.xml" rel="section"><i class="fa fa-fw fa-sitemap"></i>站点地图</a>

  </li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup">
        <div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div id="search-result">
  <div id="no-result">
    <i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>
  </div>
</div>

    </div>
  </div>

</div>
    </header>

    
  <div class="reading-progress-bar"></div>
  <a role="button" class="book-mark-link book-mark-link-fixed"></a>

  <a href="https://github.com/yourname" class="github-corner" title="Follow me on GitHub" aria-label="Follow me on GitHub" rel="noopener" target="_blank"><svg width="80" height="80" viewBox="0 0 250 250" aria-hidden="true"><path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"></path><path d="M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2" fill="currentColor" style="transform-origin: 130px 106px;" class="octo-arm"></path><path d="M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z" fill="currentColor" class="octo-body"></path></svg></a>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content post posts-expand">
            

    
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2020/03/13/Git/Git%E6%95%99%E7%A8%8B/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.jpg">
      <meta itemprop="name" content="heweiliang">
      <meta itemprop="description" content="heweiliang blog">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="heweiliang blog">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          Git教程<a href="https://github.com/user-name/repo-name/tree/branch-name/subdirectory-name_posts/Git/Git%E6%95%99%E7%A8%8B.md" class="post-edit-link" title="编辑" rel="noopener" target="_blank"><i class="fa fa-pencil"></i></a>
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              <time title="创建时间：2020-03-13 10:16:42" itemprop="dateCreated datePublished" datetime="2020-03-13T10:16:42+08:00">2020-03-13</time>
            </span>
              <span class="post-meta-item">
                <span class="post-meta-item-icon">
                  <i class="fa fa-calendar-check-o"></i>
                </span>
                <span class="post-meta-item-text">更新于</span>
                <time title="修改时间：2020-06-26 16:15:31" itemprop="dateModified" datetime="2020-06-26T16:15:31+08:00">2020-06-26</time>
              </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/Git/" itemprop="url" rel="index"><span itemprop="name">Git</span></a>
                </span>
            </span>

          
            <span class="post-meta-item" title="阅读次数" id="busuanzi_container_page_pv" style="display: none;">
              <span class="post-meta-item-icon">
                <i class="fa fa-eye"></i>
              </span>
              <span class="post-meta-item-text">阅读次数：</span>
              <span id="busuanzi_value_page_pv"></span>
            </span><br>
            <span class="post-meta-item" title="本文字数">
              <span class="post-meta-item-icon">
                <i class="fa fa-file-word-o"></i>
              </span>
                <span class="post-meta-item-text">本文字数：</span>
              <span>8.8k</span>
            </span>
            <span class="post-meta-item" title="阅读时长">
              <span class="post-meta-item-icon">
                <i class="fa fa-clock-o"></i>
              </span>
                <span class="post-meta-item-text">阅读时长 &asymp;</span>
              <span>8 分钟</span>
            </span>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <h2 id="Git教程"><a href="#Git教程" class="headerlink" title="Git教程"></a>Git教程</h2><h4 id="第一章：Git简介"><a href="#第一章：Git简介" class="headerlink" title="第一章：Git简介"></a>第一章：Git简介</h4><ul>
<li>CVS及SVN都是集中式的版本控制系统。</li>
<li>Git是分布式版本控制系统，极其强大的分支管理。</li>
</ul>
<h4 id="第二章：Git创建"><a href="#第二章：Git创建" class="headerlink" title="第二章：Git创建"></a>第二章：Git创建</h4><h6 id="Git设置name和email"><a href="#Git设置name和email" class="headerlink" title="Git设置name和email"></a>Git设置name和email</h6><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">git config --global user.name <span class="string">"Your Name"</span></span><br><span class="line">git config --global user.email <span class="string">"email@example.com"</span>		</span><br><span class="line">/*注意`git config`命令的`--global`参数，用了这个参数，表示你这台机器上所有的Git仓库都会使用这个配置，当然也可以对某个仓库指定不同的用户名和Email地址。*/</span><br></pre></td></tr></table></figure>

<h6 id="Git初始化仓库"><a href="#Git初始化仓库" class="headerlink" title="Git初始化仓库"></a>Git初始化仓库</h6><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git init</span><br></pre></td></tr></table></figure>

<h6 id="添加文件到Git仓库，分两步："><a href="#添加文件到Git仓库，分两步：" class="headerlink" title="添加文件到Git仓库，分两步："></a>添加文件到Git仓库，分两步：</h6><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">git add readme.txt  //把文件添加到仓库</span><br><span class="line">git commit -m <span class="string">"wrote a readme file"</span> // -m 后面输入的是本次提交的说明</span><br></pre></td></tr></table></figure>

<h6 id="Git工作区的状态"><a href="#Git工作区的状态" class="headerlink" title="Git工作区的状态"></a>Git工作区的状态</h6><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git status</span><br></pre></td></tr></table></figure>

<h6 id="Git查看修改内容"><a href="#Git查看修改内容" class="headerlink" title="Git查看修改内容"></a>Git查看修改内容</h6><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git diff readme.txt(文件名) //查看difference</span><br></pre></td></tr></table></figure>





<h4 id="第三章：版本功能"><a href="#第三章：版本功能" class="headerlink" title="第三章：版本功能"></a>第三章：版本功能</h4><h6 id="版本回退"><a href="#版本回退" class="headerlink" title="版本回退"></a>版本回退</h6><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">git reset --hard 版本号 //git reset --hard HEAD^  //下一个版本</span><br><span class="line">git <span class="built_in">log</span></span><br><span class="line">git reflog</span><br></pre></td></tr></table></figure>

<p>Git显示从最近到最远的提交日志<code>HEAD</code>指向的版本就是当前版本</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">git reset --hard HEAD^  //下一个版本</span><br><span class="line">git reset --hard 版本号 //版本号不用写全</span><br></pre></td></tr></table></figure>

<p><code>git log</code>可以查看提交历史，以便确定要<strong>回退到哪个版本</strong>。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">git <span class="built_in">log</span> --pretty=oneline  //输出信息太多，看得眼花缭乱的，可以试试加上--pretty=oneline参数</span><br><span class="line">1094adb7b9b3807259d8cb349e7df1d4d6477073 (HEAD -&gt; master) append GPL</span><br><span class="line">e475afc93c209a690c39c13a46716e8fa000c366 add distributed</span><br><span class="line">eaadf4e385e865d25c48e7ca9c8395c3f7dfaef0 wrote a readme file</span><br><span class="line"></span><br><span class="line">git <span class="built_in">log</span> </span><br><span class="line">commit e475afc93c209a690c39c13a46716e8fa000c366 (HEAD -&gt; master)</span><br><span class="line">Author: Michael Liao &lt;askxuefeng@gmail.com&gt;</span><br><span class="line">Date:   Fri May 18 21:03:36 2018 +0800</span><br><span class="line">    add distributed</span><br><span class="line">1094adb...的是commit id（版本号）  append GPL是版本描述 就是git commit -m <span class="string">"message"</span>的message的内容</span><br></pre></td></tr></table></figure>

<p><code>git reflog</code>查看命令历史，以便确定要<strong>回到未来的哪个版本</strong>。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git reflog</span><br></pre></td></tr></table></figure>

<h6 id="工作区和暂存区"><a href="#工作区和暂存区" class="headerlink" title="工作区和暂存区"></a>工作区和暂存区</h6><p><img src="C:%5CUsers%5CAdministrator%5CDesktop%5CStudyNote%5Cblogphoto%5C0.jpg" alt="0"></p>
<p>本地仓库 工作区（Working Directory） 就是你在电脑里能看到的目录</p>
<ul>
<li><p>版本库（Respository） </p>
<p>工作区有一个隐藏目录<code>.git</code>，这个不算工作区，而是Git的版本库。</p>
<p>Git的版本库里存了很多东西，其中最重要的就是称为stage（或者叫index）的<strong>暂存区</strong>，还有Git为我们自动创建的第一个分支<code>master</code>，以及指向<code>master</code>的一个指针叫<code>HEAD</code>。</p>
</li>
<li><p>工作区</p>
</li>
</ul>
<p>前面讲了我们把文件往Git版本库里添加的时候，是分两步执行的：</p>
<p>第一步是用<code>git add</code>把文件添加进去，实际上就是把文件修改添加到暂存区；</p>
<p>第二步是用<code>git commit</code>提交更改，实际上就是把暂存区的所有内容提交到当前分支。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">git add readme.txt</span><br><span class="line">git commit -m &quot;wrote a readme file&quot;</span><br></pre></td></tr></table></figure>

<p><code>git add</code>命令实际上就是把要提交的所有修改放到暂存区（Stage），然后，</p>
<p>执行<code>git commit</code>就可以一次性把暂存区的所有修改提交到分支。</p>
<h6 id="管理修改"><a href="#管理修改" class="headerlink" title="管理修改"></a>管理修改</h6><p>为什么Git比其他版本控制系统设计得优秀，因为<strong>Git跟踪并管理的是修改，而非文件</strong>。</p>
<p>查看工作区和版本库里面最新版本的区别</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git diff HEAD --readme.txt</span><br></pre></td></tr></table></figure>

<h6 id="撤销修改"><a href="#撤销修改" class="headerlink" title="撤销修改"></a>撤销修改</h6><p><strong>丢弃工作区的修改</strong></p>
<p>场景1：当你改乱了工作区某个文件的内容，想直接丢弃工作区的修改时，用命令<code>git checkout -- file</code>。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git checkout -- file</span><br></pre></td></tr></table></figure>

<p><strong>添加到了暂存区时，想丢弃修改</strong></p>
<p>场景2：当你不但改乱了工作区某个文件的内容，还添加到了暂存区时，想丢弃修改，分两步，第一步用命令<code>git reset HEAD</code>，就回到了场景1，第二步按场景1操作。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">git reset HEAD &lt;file&gt; </span><br><span class="line">//回退版本，也可以把暂存区的修改回退到工作区。当我们用HEAD时，表示最新的版本。</span><br><span class="line">git checkout -- file</span><br></pre></td></tr></table></figure>

<p><strong>修改到版本库</strong></p>
<p>场景3：已经提交了不合适的修改到版本库时，想要撤销本次提交，参考<a href="https://www.liaoxuefeng.com/wiki/896043488029600/897013573512192" target="_blank" rel="noopener">版本回退</a>一节，不过前提是没有推送到远程库。</p>
<h6 id="删除文件"><a href="#删除文件" class="headerlink" title="删除文件"></a>删除文件</h6><p>命令<code>git rm</code>用于删除一个文件。如果一个文件已经被提交到版本库，那么你永远不用担心误删，但是要小心，你只能恢复文件到最新版本，你会丢失<strong>最近一次提交后你修改的内容</strong>。</p>
<ol>
<li>工作区中删除文件</li>
</ol>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">rm test.txt</span><br></pre></td></tr></table></figure>



<ol start="2">
<li>要从版本库中删除该文件，那就用命令<code>git rm</code>删掉，并且<code>git commit</code>：</li>
</ol>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">git rm test.txt</span><br><span class="line">git commit -m &quot;remove test.txt&quot;</span><br></pre></td></tr></table></figure>



<ol start="3">
<li>删错了，因为版本库里还有呢，所以可以很轻松地把误删的文件恢复到最新版本：</li>
</ol>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git checkout -- test.txt</span><br></pre></td></tr></table></figure>

<p><code>git checkout</code>其实是用版本库里的版本替换工作区的版本，无论工作区是修改还是删除，都可以“一键还原”。</p>
<h4 id="第四章：分支管理"><a href="#第四章：分支管理" class="headerlink" title="第四章：分支管理"></a>第四章：分支管理</h4><p><strong>在Git里，这个分支叫主分支，即<code>master</code>分支。<code>HEAD</code>严格来说不是指向提交，而是指向<code>master</code>，<code>master</code>才是指向提交的，所以，<code>HEAD</code>指向的就是当前分支。</strong></p>
<p>Git的分支是与众不同的，无论创建、切换和删除分支，Git在1秒钟之内就能完成！无论你的版本库是1个文件还是1万个文件。 </p>
<h6 id="创建与合并分支"><a href="#创建与合并分支" class="headerlink" title="创建与合并分支"></a>创建与合并分支</h6><p><strong><code>master</code>分支是一条线，Git用<code>master</code>指向最新的提交，再用<code>HEAD</code>指向<code>master</code>，就能确定当前分支，以及当</strong></p>
<p><strong>前分支的提交点：（master===&gt;最新提交     HEAD===&gt;master）</strong></p>
<p>创+(name)切(switch -c name)删(-d)和(merge)查(branch)</p>
<p>查看分支：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git branch //branch 分支 git branch命令会列出所有分支，当前分支前面会标一个*号。</span><br></pre></td></tr></table></figure>

<p>创建分支：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git branch &lt;name&gt;</span><br></pre></td></tr></table></figure>

<p>切换分支：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git checkout &lt;name&gt; || git switch &lt;name&gt;</span><br></pre></td></tr></table></figure>

<p>我们注意到切换分支使用<code>git checkout</code>，而前面讲过的撤销修改则是<code>git checkout --</code>，同一个命令，有两种作用，确实有点令人迷惑。</p>
<p>实际上，切换分支这个动作，用<code>switch</code>更科学。因此，最新版本的Git提供了新的<code>git switch</code>命令来切换分支：</p>
<p>创建并切换分支：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">git checkout -b &lt;name&gt;</span><br><span class="line">git switch -c &lt;name&gt;</span><br></pre></td></tr></table></figure>

<p>合并某分支到当前分支：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git merge &lt;name&gt; &#x2F;&#x2F; merge 	合并, 融合, 并入</span><br></pre></td></tr></table></figure>

<p>删除分支：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git branch -d &lt;name&gt;</span><br></pre></td></tr></table></figure>

<p>强行删除分支</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git branch -D &lt;name&gt; &#x2F;&#x2F;要先合并</span><br></pre></td></tr></table></figure>

<h6 id="解决冲突"><a href="#解决冲突" class="headerlink" title="解决冲突"></a>解决冲突</h6><ul>
<li><p>当Git无法自动合并分支时，就必<strong>须首先解决冲突。解决冲突后，再提交，合并完成</strong>。</p>
</li>
<li><p><strong>解决冲突就是把Git合并失败的文件手动编辑为我们希望的内容，再提交</strong>。</p>
</li>
<li><p>用<code>git log --graph</code>命令可以看到分支合并图。</p>
<p>查看分支合并图</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">git log --graph &#x2F;&#x2F; graph 图</span><br><span class="line">git log --graph --pretty&#x3D;oneline --abbrev-commit</span><br></pre></td></tr></table></figure>

</li>
</ul>
<h6 id="分支管理策略"><a href="#分支管理策略" class="headerlink" title="分支管理策略"></a>分支管理策略</h6><p>合并分支时，加上<code>--no-ff</code>参数就可以用普通模式合并，合并后的历史有分支，能看出来曾经做过合并，而<code>fast forward</code>合并就看不出来曾经做过合并。</p>
<h6 id="Bug分支"><a href="#Bug分支" class="headerlink" title="Bug分支"></a>Bug分支</h6><ul>
<li>修复bug时，我们会通过创建新的bug分支进行修复，然后合并，最后删除；</li>
<li>当手头工作没有完成时，先把工作现场<code>git stash</code>一下，然后去修复bug，修复后，再<code>git stash pop</code>，回到工作现场；</li>
<li>在master分支上修复的bug，想要合并到当前dev分支，可以用<code>git cherry-pick</code>命令，把bug提交的修改“复制”到当前分支，避免重复劳动。</li>
</ul>
<p>每个bug都可以通过一个新的临时分支来修复，修复后，合并分支，然后将临时分支删除。</p>
<p>Git还提供了一个<code>stash</code>功能，可以把当前工作现场“储藏”起来，等以后恢复现场后继续工作：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git stash</span><br></pre></td></tr></table></figure>

<h6 id="Feature分支"><a href="#Feature分支" class="headerlink" title="Feature分支"></a>Feature分支</h6><p>添加一个新功能时，你肯定不希望因为一些实验性质的代码，把主分支搞乱了，所以，每添加一个新功能，最好新建一个feature分支，在上面开发，完成后，合并，最后，删除该feature分支。</p>
<ul>
<li><p>开发一个新feature，最好新建一个分支；</p>
</li>
<li><p>如果要<strong>丢弃一个没有被合并过的分支</strong>，可以通过<code>git branch -D</code>强行删除。</p>
</li>
<li><p>强行删除分支</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">git branch -D &lt;name&gt; &#x2F;&#x2F;要先合并</span><br><span class="line">origin  git@github.com:michaelliao&#x2F;learngit.git (fetch)</span><br><span class="line">origin  git@github.com:michaelliao&#x2F;learngit.git (push)</span><br></pre></td></tr></table></figure>

</li>
</ul>
<h6 id="多人协作"><a href="#多人协作" class="headerlink" title="多人协作"></a>多人协作</h6><ul>
<li><p>查看远程库信息，使用<code>git remote -v</code>；</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">git remote -v &#x2F;&#x2F;更详细的信息</span><br><span class="line">origin  git@github.com:michaelliao&#x2F;learngit.git (fetch)</span><br><span class="line">origin  git@github.com:michaelliao&#x2F;learngit.git (push)</span><br></pre></td></tr></table></figure>

<p>上面显示了可以抓取和推送的<code>origin</code>的地址。如果没有推送权限，就看不到push的地址。</p>
</li>
<li><p>本地新建的分支如果不推送到远程，对其他人就是不可见的；</p>
</li>
<li><p>从<strong>本地推送分支</strong>，使用<code>git push origin branch-name</code>，如果推送失败，先用<code>git pull</code>抓取远程的新提交。推送分支，就是把该分支上的所有本地提交推送到远程库。推送时，要指定本地分支，这样，Git就会把该分支推送到远程库对应的远程分支上：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">git push origin branch-name  </span><br><span class="line"></span><br><span class="line">git add env.txt</span><br><span class="line">git add env.txt</span><br><span class="line">git push origin dev</span><br></pre></td></tr></table></figure>
</li>
<li><p>在<strong>本地创建和远程分支对应的分支</strong>，使用<code>git checkout -b branch-name origin/branch-name</code>，本地和远程分支的名称最好一致；</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git checkout -b branch-name origin&#x2F;branch-name</span><br></pre></td></tr></table></figure>
</li>
<li><p><strong>建立本地分支和远程分支的关联</strong>，使用<code>git branch --set-upstream branch-name origin/branch-name</code>；</p>
</li>
<li><p>如果<code>git pull</code>提示<code>no tracking information</code>，则说明本地分支和远程分支的链接关系没有创建，用命令<code>git branch --set-upstream-to  origin/</code>。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"> git branch --set-upstream-to&#x3D;origin&#x2F;dev dev</span><br><span class="line">git branch --set-upstream branch-name origin&#x2F;branch-name</span><br></pre></td></tr></table></figure>
</li>
<li><p><strong>从远程抓取分支</strong>，使用<code>git pull</code>，如果有冲突，要先处理冲突。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git pull</span><br></pre></td></tr></table></figure>

</li>
</ul>
<p>你的小伙伴的最新提交和你试图推送的提交有冲突，解决办法也很简单，Git已经提示我们，先用<code>git pull</code>把最新的提交从<code>origin/dev</code>抓下来，然后，在本地合并，解决冲突，再推送：</p>
<p>但是，并不是一定要把本地分支往远程推送，那么，哪些分支需要推送，哪些不需要呢？</p>
<ul>
<li><code>master</code>分支是主分支，因此要时刻与远程同步；</li>
<li><code>dev</code>分支是开发分支，团队所有成员都需要在上面工作，所以也需要与远程同步；</li>
<li><code>bug</code>分支只用于在本地修复bug，就没必要推到远程了，除非老板要看看你每周到底修复了几个bug；</li>
<li><code>feature</code>分支是否推到远程，取决于你是否和你的小伙伴合作在上面开发。</li>
</ul>
<h6 id="Rebase"><a href="#Rebase" class="headerlink" title="Rebase"></a>Rebase</h6><p>Git有一种称为rebase的操作，有人把它翻译成“变基”</p>
<ul>
<li>rebase操作可以把本地未push的分叉提交历史整理成直线；</li>
<li>rebase的目的是使得我们在查看历史提交的变化时更容易，因为分叉的提交需要三方对比。</li>
</ul>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git log --graph --pretty&#x3D;oneline --abbrev-commit</span><br></pre></td></tr></table></figure>

<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git rebase</span><br></pre></td></tr></table></figure>



<h4 id="第五章：标签管理"><a href="#第五章：标签管理" class="headerlink" title="第五章：标签管理"></a>第五章：标签管理</h4><p>发布一个版本时，我们通常先在版本库中打一个标签（tag），这样，就唯一确定了打标签时刻的版本。将来无论什么时候，取某个标签的版本，就是把那个打标签的时刻的历史版本取出来。所以，标签也是版本库的一个快照。</p>
<p>Git的标签虽然是版本库的快照，但其实它就是指向某个commit的指针（跟分支很像对不对？但是分支可以移动，标签不能移动），所以，创建和删除标签都是瞬间完成的。</p>
<p>tag就是一个让人容易记住的有意义的名字，它跟某个commit绑在一起。</p>
<h5 id="创建标签"><a href="#创建标签" class="headerlink" title="创建标签"></a>创建标签</h5><ul>
<li><p>新建一个标签，默认为<code>HEAD</code>，也可以指定一个commit id；</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git tag</span><br></pre></td></tr></table></figure>
</li>
<li><p>指定标签信息；</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git tag -a -m &quot;标签信息&quot;</span><br></pre></td></tr></table></figure>
</li>
<li><p>指定标签信息；</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git tag</span><br></pre></td></tr></table></figure>

</li>
</ul>
<h5 id="操作标签"><a href="#操作标签" class="headerlink" title="操作标签"></a>操作标签</h5><ul>
<li><p>推送一个本地标签；</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git push origin</span><br></pre></td></tr></table></figure>
</li>
<li><p>推送全部未推送过的本地标签；</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git push origin --tags</span><br></pre></td></tr></table></figure>
</li>
<li><p>删除一个本地标签；</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git tag -d</span><br></pre></td></tr></table></figure>
</li>
<li><p>删除一个远程标签。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git push origin :refs&#x2F;tags&#x2F;</span><br></pre></td></tr></table></figure>





</li>
</ul>
<h4 id="第六章：远程仓库"><a href="#第六章：远程仓库" class="headerlink" title="第六章：远程仓库"></a>第六章：远程仓库</h4><h5 id="远程程库：Github、Gittee、GitLab。提供Git仓库托管服务的。"><a href="#远程程库：Github、Gittee、GitLab。提供Git仓库托管服务的。" class="headerlink" title="远程程库：Github、Gittee、GitLab。提供Git仓库托管服务的。"></a>远程程库：Github、Gittee、GitLab。提供Git仓库托管服务的。</h5><p>GitHub作为免费的远程仓库</p>
<ul>
<li>在GitHub上，可以任意Fork开源仓库；</li>
<li>自己<strong>拥有Fork后的仓库的读写权限</strong>；</li>
<li>可以推送pull request给官方仓库来贡献代码。</li>
</ul>
<p>Gittee(码云)</p>
<ul>
<li>国内的Git托管服务——<a href="https://gitee.com/?utm_source=blog_lxf" target="_blank" rel="noopener">Gitee</a>（<a href="https://gitee.com/?utm_source=blog_lxf" target="_blank" rel="noopener">gitee.com</a>）</li>
</ul>
<p>GitLab</p>
<h5 id="添加远程库"><a href="#添加远程库" class="headerlink" title="添加远程库"></a>添加远程库</h5><h5 id="从远程库克隆"><a href="#从远程库克隆" class="headerlink" title="从远程库克隆"></a>从远程库克隆</h5><h5 id="使用GitHub"><a href="#使用GitHub" class="headerlink" title="使用GitHub"></a>使用GitHub</h5><p>GitHub是免费的远程仓库、一个开源协作社区。</p>
<ul>
<li><p>在GitHub上，可以任意Fork开源仓库；</p>
<p>Fork就是把别人的仓库项目<strong>克隆</strong>在自已的仓库项目</p>
</li>
<li><p>自己<strong>拥有Fork后的仓库的读写权限</strong>；</p>
</li>
<li><p>可以推送pull request给官方仓库来贡献代码。</p>
</li>
</ul>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git <span class="built_in">clone</span> 仓库地址</span><br></pre></td></tr></table></figure>

<p>如果从别人的仓库地址克隆，因为没有权限，你将<strong>不能推送修改</strong>。</p>
<h5 id="使用Gitee（码云）"><a href="#使用Gitee（码云）" class="headerlink" title="使用Gitee（码云）"></a>使用Gitee（码云）</h5><p>国内的Git托管服务——<a href="https://gitee.com/?utm_source=blog_lxf" target="_blank" rel="noopener">Gitee</a></p>
<h6 id="上传自己的SSH公钥"><a href="#上传自己的SSH公钥" class="headerlink" title="上传自己的SSH公钥"></a>上传自己的SSH公钥</h6><p>选择右上角用户头像 -&gt; 菜单“修改资料”，然后选择“SSH公钥”，填写一个便于识别的标题，然后把用户主目录下的<code>.ssh/id_rsa.pub</code>(C:\Users\Administrator.ssh)文件的内容粘贴进去：</p>
<h6 id="配置SSH添加公钥（id-rsa-pub）到远程账号账号-id-rsa-id-rsa-pub"><a href="#配置SSH添加公钥（id-rsa-pub）到远程账号账号-id-rsa-id-rsa-pub" class="headerlink" title="配置SSH添加公钥（id_rsa.pub）到远程账号账号(id_rsa//id_rsa.pub)"></a>配置SSH添加公钥（id_rsa.pub）到远程账号账号(id_rsa//id_rsa.pub)</h6><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">// 查看有没有公钥</span><br><span class="line">C:\Users\Administrator\.ssh\id_rsa.pub</span><br><span class="line">// 没有生成公钥</span><br><span class="line">ssh-keygen -t rsa -C <span class="string">"youremail@example.com"</span> </span><br><span class="line">// 添加公钥到远程仓库</span><br></pre></td></tr></table></figure>

<h6 id="关联到Gitee的远程库"><a href="#关联到Gitee的远程库" class="headerlink" title="关联到Gitee的远程库"></a>关联到Gitee的远程库</h6><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git remote add origin 具体项目的仓库地址  //remote 远程  + origin 起源</span><br></pre></td></tr></table></figure>

<h6 id="删除已有的GitHub远程库："><a href="#删除已有的GitHub远程库：" class="headerlink" title="删除已有的GitHub远程库："></a>删除已有的GitHub远程库：</h6><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git remote rm origin &#x2F;&#x2F;git给远程库起的默认名称是&#96;origin&#96;</span><br></pre></td></tr></table></figure>

<h6 id="查看远程库信息："><a href="#查看远程库信息：" class="headerlink" title="查看远程库信息："></a>查看远程库信息：</h6><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git remote -v</span><br></pre></td></tr></table></figure>

<p>如果在使用命令<code>git remote add</code>时报错：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">git remote add origin git@gitee.com:liaoxuefeng/learngit.git</span><br><span class="line">fatal: remote origin already exists.</span><br></pre></td></tr></table></figure>

<p>这说明本地库已经关联了一个名叫<code>origin</code>的远程库，此时，可以先用删除远程库，再关联：</p>
<h6 id="一个本地库既关联GitHub，又关联Gitee"><a href="#一个本地库既关联GitHub，又关联Gitee" class="headerlink" title="一个本地库既关联GitHub，又关联Gitee"></a>一个本地库既关联GitHub，又关联Gitee</h6><p>git本身是分布式版本控制系统，可以同步到另外一个远程库，当然也可以同步到另外两个远程库。</p>
<p><strong>使用多个远程库时，我们要注意，git给远程库起的默认名称是<code>origin</code>，如果有多个远程库，我们需要用不同的名称来标识不同的远程库。</strong></p>
<p>关联GitHub的远程库：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git remote add github git@github.com:michaelliao/learngit.git</span><br></pre></td></tr></table></figure>

<p>关联Gitee的远程库：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git remote add gitee git@gitee.com:liaoxuefeng/learngit.git</span><br></pre></td></tr></table></figure>

<p>查看远程库信息，可以看到两个远程库：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">git remote -v</span><br><span class="line">gitee	git@gitee.com:liaoxuefeng/learngit.git (fetch)</span><br><span class="line">gitee	git@gitee.com:liaoxuefeng/learngit.git (push)</span><br><span class="line">github	git@github.com:michaelliao/learngit.git (fetch)</span><br><span class="line">github	git@github.com:michaelliao/learngit.git (push)</span><br></pre></td></tr></table></figure>

<p>如果要推送（上传）到GitHub，使用命令：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git push github master</span><br></pre></td></tr></table></figure>

<p>如果要推送（上传）到Gitee，使用命令：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git push gitee master</span><br></pre></td></tr></table></figure>



<h6 id="自定义Git"><a href="#自定义Git" class="headerlink" title="自定义Git"></a>自定义Git</h6><p>在<a href="https://www.liaoxuefeng.com/wiki/896043488029600/896067074338496" target="_blank" rel="noopener">安装Git</a>一节中，我们已经配置了<code>user.name</code>和<code>user.email</code>，实际上，Git还有很多可配置项。</p>
<p>比如，让<strong>Git显示颜色</strong>，会让命令输出看起来更醒目：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git config --global color.ui <span class="literal">true</span></span><br></pre></td></tr></table></figure>



<h6 id="忽略特殊文件"><a href="#忽略特殊文件" class="headerlink" title="忽略特殊文件"></a>忽略特殊文件</h6><ul>
<li><p><code>.gitignore</code>文件，把要忽略的文件名填进去，Git就会自动忽略这些文件。</p>
</li>
<li><p><code>.gitignore</code>文件本身要放到版本库里，并且可以对<code>.gitignore</code>做版本管理！</p>
</li>
</ul>
<h6 id="搭建Git服务器"><a href="#搭建Git服务器" class="headerlink" title="搭建Git服务器"></a>搭建Git服务器</h6><ul>
<li>自己搭建一台Git服务器作为私有仓库使用。</li>
<li>推荐用Ubuntu或Debian，这样，通过几条简单的<code>apt</code>命令就可以完成安装。</li>
</ul>
<ul>
<li>搭建Git服务器非常简单，通常10分钟即可完成；</li>
<li>要方便<strong>管理公钥</strong>，用<a href="https://github.com/sitaramc/gitolite" target="_blank" rel="noopener">Gitosis</a>；</li>
<li>要像SVN那样变态地<strong>控制权限</strong>，用<a href="https://github.com/sitaramc/gitolite" target="_blank" rel="noopener">Gitolite</a>。</li>
</ul>
<p><strong>第一步，安装<code>git</code></strong>：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ sudo apt-get install git</span><br></pre></td></tr></table></figure>

<p><strong>第二步，创建一个<code>git</code>用户，用来运行<code>git</code>服务：</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ sudo adduser git</span><br></pre></td></tr></table></figure>

<p><strong>第三步，创建证书登录：</strong></p>
<p><strong>收集所有需要登录的用户的公钥</strong>，就是他们自己的<code>id_rsa.pub</code>文件，把所有公钥导入到<code>/home/git/.ssh/authorized_keys</code>文件里，一行一个。</p>
<p><strong>第四步，初始化Git仓库：</strong></p>
<p>先选定一个目录作为Git仓库，假定是<code>/srv/sample.git</code>，在<code>/srv</code>目录下输入命令：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ sudo git init --bare sample.git</span><br></pre></td></tr></table></figure>

<p>Git就会创建一个裸仓库，裸仓库没有工作区，因为服务器上的Git仓库纯粹是为了共享，所以不让用户直接登录到服务器上去改工作区，并且服务器上的Git仓库通常都以<code>.git</code>结尾。然后，把owner改为<code>git</code>：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ sudo chown -R git:git sample.git</span><br></pre></td></tr></table></figure>

<p><strong>第五步，禁用shell登录：</strong></p>
<p>出于安全考虑，第二步创建的git用户不允许登录shell，这可以通过编辑<code>/etc/passwd</code>文件完成。找到类似下面的一行：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">git:x:1001:1001:,,,:&#x2F;home&#x2F;git:&#x2F;bin&#x2F;bash</span><br><span class="line">git:x:1001:1001:,,,:&#x2F;home&#x2F;git:&#x2F;usr&#x2F;bin&#x2F;git-shell</span><br></pre></td></tr></table></figure>

<p>这样，<code>git</code>用户可以正常通过ssh使用git，但无法登录shell，因为我们为<code>git</code>用户指定的<code>git-shell</code>每次一登录就自动退出。</p>
<p><strong>第六步，克隆远程仓库：</strong></p>
<p>现在，可以通过<code>git clone</code>命令克隆远程仓库了，在各自的电脑上运行：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">$ git clone git@server:&#x2F;srv&#x2F;sample.git</span><br><span class="line">Cloning into &#39;sample&#39;...</span><br><span class="line">warning: You appear to have cloned an empty repository.</span><br></pre></td></tr></table></figure>



<h6 id="使用SourceTree-GUI工具"><a href="#使用SourceTree-GUI工具" class="headerlink" title="使用SourceTree(GUI工具)"></a>使用SourceTree(GUI工具)</h6><p>熟练使用命令操作Git后，再使用GUI工具，就可以更高效。</p>
<h4 id="解决问题"><a href="#解决问题" class="headerlink" title="解决问题"></a>解决问题</h4><h6 id="Git配置免密登录两个配置方案："><a href="#Git配置免密登录两个配置方案：" class="headerlink" title="Git配置免密登录两个配置方案："></a>Git配置免密登录两个配置方案：</h6><ol>
<li><p>切换成git方式,<a href="http://blog.csdn.net/lqlqlq007/article/details/78983879" target="_blank" rel="noopener"> git ssh key配置</a></p>
<p>如上：<strong>生成id_rsa.pub时不要输入密码</strong></p>
</li>
<li><p>配置credential.helper：</p>
<p>首先简单介绍一下credential.helper这个配置项的含义：这个配置项允许用户自行指定git所使用的凭据管理工具。<br>简单粗暴的办法就是直接配置credential.helper的值为manager（注意：当你需要在同一台机器上使用多个git账号这么搞就不行了，因为2个账号必定使用的不同的凭据）：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git config credential.helper manager</span><br></pre></td></tr></table></figure>


</li>
</ol>
<p>   再次尝试pull代码的时候会弹出窗口要求输入用户名密码（只需要输入这一次就ok了）：</p>
<p>Sourcetree + gitee(码云) 使用教程</p>
<p><a href="https://www.wangquanwei.com/68.html" target="_blank" rel="noopener">https://www.wangquanwei.com/68.html</a></p>

    </div>

    
    
    
        <div class="reward-container">
  <div></div>
  <button onclick="var qr = document.getElementById('qr'); qr.style.display = (qr.style.display === 'none') ? 'block' : 'none';">
    打赏
  </button>
  <div id="qr" style="display: none;">
      
      <div style="display: inline-block;">
        <img src="/images/wechatpay.jpg" alt="heweiliang 微信支付">
        <p>微信支付</p>
      </div>
      
      <div style="display: inline-block;">
        <img src="/images/alipay.jpg" alt="heweiliang 支付宝">
        <p>支付宝</p>
      </div>

  </div>
</div>

        

  <div class="followme">
    <p>欢迎关注我的其它发布渠道</p>

    <div class="social-list">

        <div class="social-item">
          <a target="_blank" class="social-link" href="/atom.xml">
            <span class="icon">
              <i class="fa fa-rss"></i>
            </span>

            <span class="label">RSS</span>
          </a>
        </div>
    </div>
  </div>


      <footer class="post-footer">
          <div class="post-tags">
              <a href="/tags/Git/" rel="tag"># Git</a>
          </div>

        


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/2020/03/13/%E8%AE%A1%E7%AE%97%E6%9C%BA%E5%9F%BA%E7%A1%80/V2RAY+WS+TLS%E5%AE%89%E8%A3%85/" rel="prev" title="计算机基础/V2RAY+WS+TLS安装">
      <i class="fa fa-chevron-left"></i> 计算机基础/V2RAY+WS+TLS安装
    </a></div>
      <div class="post-nav-item">
    <a href="/2020/03/13/Git/Git%E5%B8%B8%E7%94%A8%E5%91%BD%E4%BB%A4/" rel="next" title="Git的常用命令">
      Git的常用命令 <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  



          </div>
          
    
  <div class="comments">
    <div id="lv-container" data-id="city" data-uid="MTAyMC81MDY1MC8yNzEzMw=="></div>
  </div>
  

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#Git教程"><span class="nav-number">1.</span> <span class="nav-text">Git教程</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#第一章：Git简介"><span class="nav-number">1.0.1.</span> <span class="nav-text">第一章：Git简介</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#第二章：Git创建"><span class="nav-number">1.0.2.</span> <span class="nav-text">第二章：Git创建</span></a><ol class="nav-child"><li class="nav-item nav-level-6"><a class="nav-link" href="#Git设置name和email"><span class="nav-number">1.0.2.0.1.</span> <span class="nav-text">Git设置name和email</span></a></li><li class="nav-item nav-level-6"><a class="nav-link" href="#Git初始化仓库"><span class="nav-number">1.0.2.0.2.</span> <span class="nav-text">Git初始化仓库</span></a></li><li class="nav-item nav-level-6"><a class="nav-link" href="#添加文件到Git仓库，分两步："><span class="nav-number">1.0.2.0.3.</span> <span class="nav-text">添加文件到Git仓库，分两步：</span></a></li><li class="nav-item nav-level-6"><a class="nav-link" href="#Git工作区的状态"><span class="nav-number">1.0.2.0.4.</span> <span class="nav-text">Git工作区的状态</span></a></li><li class="nav-item nav-level-6"><a class="nav-link" href="#Git查看修改内容"><span class="nav-number">1.0.2.0.5.</span> <span class="nav-text">Git查看修改内容</span></a></li></ol></li></ol></li><li class="nav-item nav-level-4"><a class="nav-link" href="#第三章：版本功能"><span class="nav-number">1.0.3.</span> <span class="nav-text">第三章：版本功能</span></a><ol class="nav-child"><li class="nav-item nav-level-6"><a class="nav-link" href="#版本回退"><span class="nav-number">1.0.3.0.1.</span> <span class="nav-text">版本回退</span></a></li><li class="nav-item nav-level-6"><a class="nav-link" href="#工作区和暂存区"><span class="nav-number">1.0.3.0.2.</span> <span class="nav-text">工作区和暂存区</span></a></li><li class="nav-item nav-level-6"><a class="nav-link" href="#管理修改"><span class="nav-number">1.0.3.0.3.</span> <span class="nav-text">管理修改</span></a></li><li class="nav-item nav-level-6"><a class="nav-link" href="#撤销修改"><span class="nav-number">1.0.3.0.4.</span> <span class="nav-text">撤销修改</span></a></li><li class="nav-item nav-level-6"><a class="nav-link" href="#删除文件"><span class="nav-number">1.0.3.0.5.</span> <span class="nav-text">删除文件</span></a></li></ol></li></ol></li><li class="nav-item nav-level-4"><a class="nav-link" href="#第四章：分支管理"><span class="nav-number">1.0.4.</span> <span class="nav-text">第四章：分支管理</span></a><ol class="nav-child"><li class="nav-item nav-level-6"><a class="nav-link" href="#创建与合并分支"><span class="nav-number">1.0.4.0.1.</span> <span class="nav-text">创建与合并分支</span></a></li><li class="nav-item nav-level-6"><a class="nav-link" href="#解决冲突"><span class="nav-number">1.0.4.0.2.</span> <span class="nav-text">解决冲突</span></a></li><li class="nav-item nav-level-6"><a class="nav-link" href="#分支管理策略"><span class="nav-number">1.0.4.0.3.</span> <span class="nav-text">分支管理策略</span></a></li><li class="nav-item nav-level-6"><a class="nav-link" href="#Bug分支"><span class="nav-number">1.0.4.0.4.</span> <span class="nav-text">Bug分支</span></a></li><li class="nav-item nav-level-6"><a class="nav-link" href="#Feature分支"><span class="nav-number">1.0.4.0.5.</span> <span class="nav-text">Feature分支</span></a></li><li class="nav-item nav-level-6"><a class="nav-link" href="#多人协作"><span class="nav-number">1.0.4.0.6.</span> <span class="nav-text">多人协作</span></a></li><li class="nav-item nav-level-6"><a class="nav-link" href="#Rebase"><span class="nav-number">1.0.4.0.7.</span> <span class="nav-text">Rebase</span></a></li></ol></li></ol></li><li class="nav-item nav-level-4"><a class="nav-link" href="#第五章：标签管理"><span class="nav-number">1.0.5.</span> <span class="nav-text">第五章：标签管理</span></a><ol class="nav-child"><li class="nav-item nav-level-5"><a class="nav-link" href="#创建标签"><span class="nav-number">1.0.5.1.</span> <span class="nav-text">创建标签</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#操作标签"><span class="nav-number">1.0.5.2.</span> <span class="nav-text">操作标签</span></a></li></ol></li><li class="nav-item nav-level-4"><a class="nav-link" href="#第六章：远程仓库"><span class="nav-number">1.0.6.</span> <span class="nav-text">第六章：远程仓库</span></a><ol class="nav-child"><li class="nav-item nav-level-5"><a class="nav-link" href="#远程程库：Github、Gittee、GitLab。提供Git仓库托管服务的。"><span class="nav-number">1.0.6.1.</span> <span class="nav-text">远程程库：Github、Gittee、GitLab。提供Git仓库托管服务的。</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#添加远程库"><span class="nav-number">1.0.6.2.</span> <span class="nav-text">添加远程库</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#从远程库克隆"><span class="nav-number">1.0.6.3.</span> <span class="nav-text">从远程库克隆</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#使用GitHub"><span class="nav-number">1.0.6.4.</span> <span class="nav-text">使用GitHub</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#使用Gitee（码云）"><span class="nav-number">1.0.6.5.</span> <span class="nav-text">使用Gitee（码云）</span></a><ol class="nav-child"><li class="nav-item nav-level-6"><a class="nav-link" href="#上传自己的SSH公钥"><span class="nav-number">1.0.6.5.1.</span> <span class="nav-text">上传自己的SSH公钥</span></a></li><li class="nav-item nav-level-6"><a class="nav-link" href="#配置SSH添加公钥（id-rsa-pub）到远程账号账号-id-rsa-id-rsa-pub"><span class="nav-number">1.0.6.5.2.</span> <span class="nav-text">配置SSH添加公钥（id_rsa.pub）到远程账号账号(id_rsa&#x2F;&#x2F;id_rsa.pub)</span></a></li><li class="nav-item nav-level-6"><a class="nav-link" href="#关联到Gitee的远程库"><span class="nav-number">1.0.6.5.3.</span> <span class="nav-text">关联到Gitee的远程库</span></a></li><li class="nav-item nav-level-6"><a class="nav-link" href="#删除已有的GitHub远程库："><span class="nav-number">1.0.6.5.4.</span> <span class="nav-text">删除已有的GitHub远程库：</span></a></li><li class="nav-item nav-level-6"><a class="nav-link" href="#查看远程库信息："><span class="nav-number">1.0.6.5.5.</span> <span class="nav-text">查看远程库信息：</span></a></li><li class="nav-item nav-level-6"><a class="nav-link" href="#一个本地库既关联GitHub，又关联Gitee"><span class="nav-number">1.0.6.5.6.</span> <span class="nav-text">一个本地库既关联GitHub，又关联Gitee</span></a></li><li class="nav-item nav-level-6"><a class="nav-link" href="#自定义Git"><span class="nav-number">1.0.6.5.7.</span> <span class="nav-text">自定义Git</span></a></li><li class="nav-item nav-level-6"><a class="nav-link" href="#忽略特殊文件"><span class="nav-number">1.0.6.5.8.</span> <span class="nav-text">忽略特殊文件</span></a></li><li class="nav-item nav-level-6"><a class="nav-link" href="#搭建Git服务器"><span class="nav-number">1.0.6.5.9.</span> <span class="nav-text">搭建Git服务器</span></a></li><li class="nav-item nav-level-6"><a class="nav-link" href="#使用SourceTree-GUI工具"><span class="nav-number">1.0.6.5.10.</span> <span class="nav-text">使用SourceTree(GUI工具)</span></a></li></ol></li></ol></li><li class="nav-item nav-level-4"><a class="nav-link" href="#解决问题"><span class="nav-number">1.0.7.</span> <span class="nav-text">解决问题</span></a><ol class="nav-child"><li class="nav-item nav-level-6"><a class="nav-link" href="#Git配置免密登录两个配置方案："><span class="nav-number">1.0.7.0.1.</span> <span class="nav-text">Git配置免密登录两个配置方案：</span></a></li></ol></li></ol></li></ol></li></ol></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="heweiliang"
      src="/images/avatar.jpg">
  <p class="site-author-name" itemprop="name">heweiliang</p>
  <div class="site-description" itemprop="description">heweiliang blog</div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/archives/">
        
          <span class="site-state-item-count">96</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/categories/">
          
        <span class="site-state-item-count">18</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/tags/">
          
        <span class="site-state-item-count">24</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>
  <div class="links-of-author motion-element">
      <span class="links-of-author-item">
        <a href="https://github.com/yourname" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;yourname" rel="noopener" target="_blank"><i class="fa fa-fw fa-github"></i></a>
      </span>
      <span class="links-of-author-item">
        <a href="mailto:yourname@gmail.com" title="E-Mail → mailto:yourname@gmail.com" rel="noopener" target="_blank"><i class="fa fa-fw fa-envelope"></i></a>
      </span>
      <span class="links-of-author-item">
        <a href="https://plus.google.com/yourname" title="Google → https:&#x2F;&#x2F;plus.google.com&#x2F;yourname" rel="noopener" target="_blank"><i class="fa fa-fw fa-google"></i></a>
      </span>
      <span class="links-of-author-item">
        <a href="https://stackoverflow.com/yourname" title="StackOverflow → https:&#x2F;&#x2F;stackoverflow.com&#x2F;yourname" rel="noopener" target="_blank"><i class="fa fa-fw fa-stack-overflow"></i></a>
      </span>
  </div>
  <div class="cc-license motion-element" itemprop="license">
    <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" class="cc-opacity" rel="noopener" target="_blank"><img src="/images/cc-by-nc-sa.svg" alt="Creative Commons"></a>
  </div>


  <div class="links-of-blogroll motion-element">
    <div class="links-of-blogroll-title">
      <i class="fa fa-fw fa-link"></i>
      Links
    </div>
    <ul class="links-of-blogroll-list">
        <li class="links-of-blogroll-item">
          <a href="http://yoursite.com/" title="http:&#x2F;&#x2F;yoursite.com">Title</a>
        </li>
        <li class="links-of-blogroll-item">
          <a href="http://yoursite.com/" title="http:&#x2F;&#x2F;yoursite.com">Title2</a>
        </li>
        <li class="links-of-blogroll-item">
          <a href="http://yoursite.com/" title="http:&#x2F;&#x2F;yoursite.com">Title3</a>
        </li>
    </ul>
  </div>

      </div>
        <div class="back-to-top motion-element">
          <i class="fa fa-arrow-up"></i>
          <span>0%</span>
        </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        

<div class="copyright">
  
  &copy; 
  <span itemprop="copyrightYear">2020</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">heweiliang</span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-area-chart"></i>
    </span>
    <span title="站点总字数">376k</span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-coffee"></i>
    </span>
    <span title="站点阅读时长">5:42</span>
</div>

        
<div class="busuanzi-count">
  <script async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
    <span class="post-meta-item" id="busuanzi_container_site_uv" style="display: none;">
      <span class="post-meta-item-icon">
        <i class="fa fa-user"></i>
      </span>
      <span class="site-uv" title="总访客量">
        <span id="busuanzi_value_site_uv"></span>
      </span>
    </span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item" id="busuanzi_container_site_pv" style="display: none;">
      <span class="post-meta-item-icon">
        <i class="fa fa-eye"></i>
      </span>
      <span class="site-pv" title="总访问量">
        <span id="busuanzi_value_site_pv"></span>
      </span>
    </span>
</div>








      </div>
    </footer>
  </div>

  
  <script src="/lib/anime.min.js"></script>
  <script src="/lib/velocity/velocity.min.js"></script>
  <script src="/lib/velocity/velocity.ui.min.js"></script>

<script src="/js/utils.js"></script>

<script src="/js/motion.js"></script>


<script src="/js/schemes/pisces.js"></script>


<script src="/js/next-boot.js"></script>

<script src="/js/bookmark.js"></script>


  <script defer src="/lib/three/three.min.js"></script>
    <script defer src="/lib/three/three-waves.min.js"></script>
    <script defer src="/lib/three/canvas_lines.min.js"></script>


  




  
<script src="/js/local-search.js"></script>













  

  

<script>
NexT.utils.loadComments(document.querySelector('#lv-container'), () => {
  window.livereOptions = {
    refer: location.pathname.replace(CONFIG.root, '').replace('index.html', '')
  };
  (function(d, s) {
    var j, e = d.getElementsByTagName(s)[0];
    if (typeof LivereTower === 'function') { return; }
    j = d.createElement(s);
    j.src = 'https://cdn-city.livere.com/js/embed.dist.js';
    j.async = true;
    e.parentNode.insertBefore(j, e);
  })(document, 'script');
});
</script>

<script src="/live2dw/lib/L2Dwidget.min.js?094cbace49a39548bed64abff5988b05"></script><script>L2Dwidget.init({"pluginModelPath":"assets/","model":{"jsonPath":"/live2dw/assets/Epsilon2.1.model.json"},"display":{"position":"left","width":300,"height":600},"mobile":{"show":false},"log":false,"pluginJsPath":"lib/","pluginRootPath":"live2dw/","tagMode":false});</script></body>
</html>
